#include "Manager.h"
ModelManager::ModelManager(void)
:Manager("ModelManager")
{
}

ModelManager::~ModelManager(void)
{
    Log();
}
TextureManager::TextureManager(void)
:Manager("TextureManager")
{
}

TextureManager::~TextureManager(void)
{
    Log();
}

bool ModelManager::AddObj(int32 mid, int32 tid,char *name, void* data)
{
    assert(mid&&name);
    if(HasObj(mid)>=0)
        return false;
    OBJ o;
    o.mid=mid;
    o.tid=tid;
    size_t size=strlen(name);
    o.name=new char[size+1];
    strcpy_s(o.name,size+1,name);
    o.model=(Model*)data;
    o.id=m_cur++;
    m_data.push_back(o);
    return true;
}

Model* ModelManager::GetModel(int32 id)
{
    if(m_data.size()>id)
        return m_data[id].model;
    return NULL;
}

void ModelManager::Log()
{
    _log("Model:\n");
    for(vector<OBJ>::iterator iter=m_data.begin();iter<m_data.end();iter++)
    {
        char tmp[256];
        sprintf(tmp,"id:%d\n",iter->id);
        _log(tmp);
        sprintf(tmp,"model name:%s\n",iter->name);
        _log(tmp);
    }
}

int32 ModelManager::HasObj(int32 id)
{
    for(vector<OBJ>::iterator iter=m_data.begin();iter<m_data.end();iter++)
    {
        if(iter->mid==id)
            return iter->id;
    }
    return -1;
}

int32 TextureManager::HasObj(char*name)
{
    for(vector<TEXTURE>::iterator iter=m_data.begin();iter<m_data.end();iter++)
    {
        if(strcmp(iter->name,name)==0)
        {
            return iter->id;
        }
    }
    return -1;
}



int32 TextureManager::AddObj(char* name,LPDIRECT3DTEXTURE9 tex)
{
    int32 id;
    if((id=HasObj(name))>=0)
    {
        return id;
    }
    TEXTURE tmp;
    tmp.id=m_cur++;
    size_t size=strlen(name);
    tmp.name=new char[size+1];
    strcpy_s(tmp.name,size+1,name);
    tmp.tex=tex;
    m_data.push_back(tmp);
    return tmp.id;
}

void TextureManager::Log()
{
    _log("Texture:\n");
    for(vector<TEXTURE>::iterator iter=m_data.begin();iter<m_data.end();iter++)
    {
        char tmp[1024];
        sprintf(tmp,"id:%d\ttexture name:%s\ttex:\n",
            iter->id,iter->name,iter->tex);
        _log(tmp);
    }
}
